#ifndef DAQ322_H
#define DAQ322_H

#include "libdaq/device/DAQ125/DAQ125.h"

namespace libdaq {
namespace device{
#define DAQ322_USB_VENDOR_ID 0x05B4
#define DAQ322_USB_PRODUCT_ID 0x00F1
#ifdef DAQ_IPC_VERSION
// 上位机&二次开发采集使用6/2端点
#define DAQ322_USB_END_POINT_IN {0x86}
#define DAQ322_USB_END_POINT_OUT {0x02}
#elif defined(UPGRADE_TOOL_VERSION)
// 升级工具使用8/4端点
#define DAQ322_USB_END_POINT_IN {0x88}
#define DAQ322_USB_END_POINT_OUT {0x04}
#else
#define DAQ322_USB_END_POINT_IN {0x86}
#define DAQ322_USB_END_POINT_OUT {0x02}
#endif
class DAQ322 : public DAQ125
{
public:
  DAQ322();
  virtual ~DAQ322() = default;
  static backend::USBOption GetUSBOption() { return libdaq::backend::USBOption(DAQ322_USB_VENDOR_ID, DAQ322_USB_PRODUCT_ID, DAQ322_USB_END_POINT_IN, DAQ322_USB_END_POINT_OUT); }

  bool InitializeDevice() override;
  bool StartADCCollection() override;
  bool StopADCCollection() override;

  bool ConfigCalibration(const std::vector<uint8_t> &data) override;
  bool ConfigADCSampleRate(LockzhinerADCSampleRate sample_rate) override;
  bool ConfigADCTriggerLength(uint32_t data_length) override;

  bool ConfigOffsetCalibration_2_5(const std::vector<uint8_t> &state) override;
  bool ConfigGetOffsetCalibration_2_5(std::vector<uint8_t>& state) override;
  bool ConfigSaveOffsetCalibration_2_5(const std::vector<uint8_t> &state) override;

  bool ConfigOffsetCalibration_5(const std::vector<uint8_t> &state) override; // 下发偏置校正参数(5V)
  bool ConfigGetOffsetCalibration_5(std::vector<uint8_t> &state) override; // 获取偏置校正参数(5V)
  bool ConfigSaveOffsetCalibration_5(const std::vector<uint8_t> &state) override; // 保存偏置校正参数(5V)

  bool ConfigOffsetCalibrationX10(const std::vector<uint8_t> &state) override; // 下发偏置校正x10档
  bool ConfigGetOffsetCalibrationX10(std::vector<uint16_t> &state) override; // 获取偏置校正x10档
  bool ConfigSaveOffsetCalibrationX10(const std::vector<uint8_t> &state) override; // 保存偏置校正x10档

  bool CloseCalibration() override;

  bool SetGPI(std::vector<uint8_t>& gpio_state) override; // 配置GPO工作模式
  bool GetGPI(std::vector<uint8_t>& gpio_state, std::vector<bool>& gpio_trigger_output) override; // 获取GPIO状态，返回<gpio_state, gpio_trigger_output>
  bool EnableGPITriggerOutput(std::vector<bool> gpio_trigger_output) override; // 使能GPO触发输出

protected:
  bool SendConfigTrigger(LockzhinerADCTriggerType type,
                         LockzhinerADCTriggerMode mode,
                         LockzhinerADCChannel channel,
                         int level) override; // 设置触发方式、触发模式、触发通道

  bool ReadDataFromBackend(int timeout) override; // 从后端读取数据
  bool SystemOpRestoreFactoryConfig() override; // 恢复出厂设置

  bool ParseCommandPacket(std::vector<unsigned char> &temp_parse_buffer, unsigned char &packet_index) override;
  bool ParseDataPacket(std::vector<unsigned char> &temp_parse_buffer, unsigned char &packet_index) override;

  bool SendSaveOffsetCalibrationX10(const std::vector<uint8_t> &state) override; // 保存偏置校正x10档

  bool RecvConnectDeviceFunc(uint8_t* str) override;
  bool RecvGetOffsetCalibration_2_5(uint8_t* str);
  bool RecvGetOffsetCalibration_5(uint8_t* str);
  bool RecvGetOffsetCalibration_10(uint8_t* str);
  bool RecvGetGPI(uint8_t* str);
protected:
  int sample_counter_ = 0; // 抽样计数器，用于低采样率抽样处理
};


}
}

#endif // DAQ322_H
